/**
* Copyright © 2018-2019, by 晓叹星沉.
*/
package org.aurora.aurora.springcloud.client;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

/**
 * 
 * <p>
 * 此处填写类说明
 * </p>
 * @author 晓叹星沉
 * @since jdk1.8
 * 2019年7月16日
 *  
 */

public class TokenFilter extends ZuulFilter {

	/** 
	 * {@inheritDoc}   
	 * @see com.netflix.zuul.IZuulFilter#shouldFilter() 
	 */
	@Override
	public boolean shouldFilter() {
		// TODO Auto-generated method stub
		return true;
	}

	/** 
	 * {@inheritDoc}   
	 * @see com.netflix.zuul.IZuulFilter#run() 
	 */
	@Override
	public Object run() throws ZuulException {
		RequestContext context = RequestContext.getCurrentContext();
        HttpServletRequest request = context.getRequest();
        String token = request.getParameter("name");
        if(StringUtils.isEmpty(token)){
        	context.setSendZuulResponse(false);
            context.setResponseStatusCode(401);
            context.setResponseBody("unAuthrized");
            return null;
        }
		return null;
	}

	/** 
	 * {@inheritDoc}   
	 * @see com.netflix.zuul.ZuulFilter#filterType() 
	 */
	@Override
	public String filterType() {
		// TODO Auto-generated method stub
		return "pre";
	}

	/** 
	 * {@inheritDoc}   
	 * @see com.netflix.zuul.ZuulFilter#filterOrder() 
	 */
	@Override
	public int filterOrder() {
		// TODO Auto-generated method stub
		return 0;
	}

}
